package io.itit.ecp.admin.file.config;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import io.itit.ecp.admin.file.AliOssProperties;
import io.itit.ecp.admin.file.service.impl.AliOssFileServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(value = {AliOssProperties.class})
public class FileConfiguration {

    private final AliOssProperties aliOssProperties;

    @Bean
    public OSS ossClient() {
        return OSSClientBuilder.create()
                .endpoint(aliOssProperties.getEndpoint())
                .credentialsProvider(new DefaultCredentialProvider(aliOssProperties.getAccessKey(), aliOssProperties.getSecretKey()))
                .build();
    }

    @Bean
    public AliOssFileServiceImpl fileService() {
        return new AliOssFileServiceImpl(aliOssProperties, ossClient());
    }
}
